
 /*
 类别管理页
author:
time : 2021年10月26日
 */ 
<template>
  <div>
    <!-- 搜索框 -->
    <div style="margin-top: 15px">
      <el-input placeholder="请输入内容" v-model="input" class="input">
        <el-button
          class="btn"
          slot="append"
          icon="el-icon-search"
          @click="search(keyName)"
        ></el-button>
      </el-input>
      <button class="add" type="buttom" @click="add()">
        <i class="icon el-icon-plus"></i> 增加类别
      </button>
    </div>
    <!-- 表格 -->
    <div class="content_table">
      <el-table :data="adminList" style="width: 99%">
        <el-table-column label="ID" :width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="类型图片">
          <template slot-scope="scope">
            <!-- <span>{{ scope.row.class_image }}</span> -->
            <img :src="`${imageName}${scope.row.class_image}`" alt="" onerror = null/>
          </template>
        </el-table-column>
         <el-table-column label="金刚区图片">
          <template slot-scope="scope">
            <!-- <span>{{ scope.row.class_image }}</span> -->
            <img :src="`${imageName}${scope.row.District}`" alt="" onerror = null/>
          </template>
        </el-table-column>

        <el-table-column label="手机类别">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <span>{{ scope.row.class_name }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="edit(scope.row)"
              >编辑</el-button
            >
            <!-- <el-button size="mini" type="danger" @click="del(scope.row.id)"
              >删除</el-button
            > -->
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除这一条数据吗？"
              @onConfirm="del(scope.row.id)"
            >
              <el-button slot="reference" size="mini" type="danger"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 模态框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="上传图片" :label-width="formLabelWidth">
          <input style="width: 65%" type="file" @change="addImg($event)" />
          <span id="photo"
            ><img :src="`${imageName}${imageUrl}${form.class_image}`"
          /></span>
        </el-form-item>
          <el-form-item label="金刚区" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            action="http://192.168.3.180:7001/api/uploadPicture"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="手机类别" :label-width="formLabelWidth">
          <el-input v-model="form.class_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="轮播顺序" :label-width="formLabelWidth">
          <el-input v-model="form.serialnumber" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="sbumit()">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCate,
  postCate,
  putCate,
  deleteCate,
  postupLoad,
} from "@/api/table";

export default {
  components: {
    // Page
  },

  data() {
    return {
      input: "",
      value: "100",
      // 是否启用,默认启用
      value1: true,
      adminList: [],
      //查询关键字
      keyName: "",

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        id: null,
        class_name: "",
        class_image: "public/img/1636359422948.png",
        District:"",
        serialnumber:"",
      },
      // 模态框输入长度
      formLabelWidth: "120px",
      // 判断是新增加还是修改,默认是添加
      isEdit: false,
      imageUrl: "",
      imageName: "http://192.168.3.180:7001/",
    };
  },

  created() {
    this.getDate();
  },

  methods: {
    handleCurrentChange() {
      this.getDate();
    },
      handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file.response.data);
      console.log(11111);
      this.form.District = file.response.data;
    },
    // 获取类别数据的方法
    getDate() {
      console.log(this.page);
      const params = {
        page: this.page,
        number: this.pageSize,
        keyname: this.keyName,
      };
      getCate(params).then((res) => {
        // console.log(res);
        if (res.code === 0) {
          this.adminList = res.data;
          this.count = res.count;
        }
        if (res.code === 4004) {
          this.adminList = res.data;
          this.count = res.count;
        }
      });
    },
    // 上传/编辑图片
    addImg(event) {
      const i = event.target.files[0];
      const formdata = new FormData();
      formdata.append("one", i);
      postupLoad(formdata).then((res) => {
        console.log(res.data);
        if (res.code === 0) {
          this.form.class_image = res.data;
          console.log(this.form.class_image);
        }
      });
    },
    // 根据关键字查询
    search(keyName) {
      this.keyName = keyName;
      this.getDate();
    },
    // 新增类别
    add() {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = true;
    },
    // 编辑类别
    edit(data) {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = false;
      this.form.id = data.id;
      this.form.class_name = data.class_name;
      this.form.class_image = data.class_image;
      this.form.District = data.District;
      this.form.serialnumber = data.serialnumber;

      
    },
    // 删除类别
    del(id) {
      // this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning",
      // })
      //   .then(() => {
      deleteCate(id).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 删除后重新渲染
          this.getDate();
        }
      });
      // })
      // .catch(() => {
      //   this.$message({
      //     type: "info",
      //     message: "已取消删除",
      //   });
      // });
    },
    // 点击提交
    sbumit() {
      // 添加到数据库
      let params = {};
      if (this.isEdit) {
        params = { ...this.form };
        console.log(params);

        postCate(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "添加类别成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("添加类别失败");
          }
        });
      } else {
        params.id = this.form.id;
        params.class_name = this.form.class_name;
        params.class_image = this.form.class_image;
        params.District = this.form.District;
        params.serialnumber = this.form.serialnumber;

        putCate(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "编辑类别成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("编辑类别失败");
          }
        });
      }
    },

    close() {
      this.dialogFormVisible = false;
      this.form.class_name = "";
      this.form.class_image = "public/img/1636359422948.png";
      this.form.District = "";
      this.form.serialnumber = "";

    },
  },
};
</script>

<style lang="scss" scoped>
.fa {
  margin-right: 5px;
}

.bread {
  width: 100%;
  height: 30px;
  padding: 10px 0;
  display: flex;
  padding-left: 10px;
  align-items: center;
  .el-breadcrumb {
    line-height: 30px;
    width: 100%;
    background-color: #fff;
  }
}
.input {
  background-color: #fff;
  width: 300px;

  margin: 30px 0px 10px 80px;
}
.add {
  background-color: rgb(133, 190, 228);
  color: white;
  border: none;
  line-height: 38px;
  border-radius: 5px;
  margin-left: 20px;
}
.content_table {
  width: 90%;

  margin: 10px auto;
}
th {
  width: 30%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
img {
  width: 70px;
  height: 60px;
}
.el-button {
  margin-right: 10px;
}
</style> 